package com.wolf.controller;

import com.wolf.pojo.Wl_user_admin;
import com.wolf.repository.Wl_user_adminMapper;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class Wl_user_adminController {

    @Autowired
    Wl_user_adminMapper wl_user_adminMapper;

    /**
     * 查询所有
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_user_admin_getAll")
    public String getAll(ModelMap modelMap){
        List<Wl_user_admin> list = wl_user_adminMapper.getAll();
        modelMap.addAttribute("wl_user_admin_list",list);
        return "table_Wl_user_admin";
    }

    /**
     * 登录
     * @return
     */
    @RequestMapping("wl_user_admin_login")
    public String login(HttpServletRequest request, String userName, String passWord){

//        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(userName,passWord);
//        Subject subject = SecurityUtils.getSubject();
//
//        try {
//            subject.login(usernamePasswordToken);   //完成登录
//            Wl_user_admin user=(Wl_user_admin) subject.getPrincipal();
//            request.getSession().setAttribute("user", user);
//            return "index";
//        } catch(Exception e) {
//            return "login";//返回登录页面
//        }

        Wl_user_admin wl_user_admin = wl_user_adminMapper.login(userName,passWord);
        if(wl_user_admin==null){
            return "login_v2";
        }else {
            request.getSession().setAttribute("wl_user_admin",wl_user_admin);
            return "index";
        }
    }

    /**
     * 删除
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_user_admin_remove")
    public String remove(ModelMap modelMap,String id){
        int num = wl_user_adminMapper.deleteByPrimaryKey(id);
        if(num>0){
            return "redirect:wl_user_admin_getAll";
        }else{
            return "500";
        }
    }

    /**
     * 新增
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_user_admin_insert")
    public String insert(ModelMap modelMap,Wl_user_admin wl_user_admin){
        //设置主键的值 值为当前日期
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String now_date = df.format(new Date());
        wl_user_admin.setId(now_date);
        int num = wl_user_adminMapper.insert(wl_user_admin);
        if(num>0){
            return "redirect:wl_user_admin_getAll";
        }else{
            return "500";
        }
    }


    /**
     * 修改
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_user_admin_getById")
    public String getById(ModelMap modelMap,String id){
        Wl_user_admin wl_user_admin = wl_user_adminMapper.selectByPrimaryKey(id);
        modelMap.addAttribute("wl_user_admin_getByAdmin",wl_user_admin);
        return "Wl_userAdmin_Update";
    }
    @RequestMapping("wl_user_admin_update")
    public String update(ModelMap modelMap,Wl_user_admin wl_user_admin){
        int num = wl_user_adminMapper.updateByPrimaryKey(wl_user_admin);
        if(num>0){
            return "redirect:wl_user_admin_getAll";
        }else {
            return "500";
        }
    }
}
